package com.ruoyi.web.controller.warehouse;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.warehouse.domain.LwSfpWarehouse;
import com.ruoyi.warehouse.service.ILwSfpWarehouseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 半成品仓库管理Controller
 * 
 * @author sll
 * @date 2023-09-07
 */
@RestController
@RequestMapping("/sfpWarehouse/sfpWarehouse")
public class LwSfpWarehouseController extends BaseController
{
    @Autowired
    private ILwSfpWarehouseService lwSfpWarehouseService;

    /**
     * 查询半成品仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:sfpWarehouse:list')")
    @GetMapping("/list")
    public TableDataInfo list(LwSfpWarehouse lwSfpWarehouse)
    {
        startPage();
        List<LwSfpWarehouse> list = lwSfpWarehouseService.selectLwSfpWarehouseList(lwSfpWarehouse);
        return getDataTable(list);
    }

    /**
     * 导出半成品仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('sfpWarehouse:sfpWarehouse:export')")
    @Log(title = "半成品仓库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LwSfpWarehouse lwSfpWarehouse)
    {
        List<LwSfpWarehouse> list = lwSfpWarehouseService.selectLwSfpWarehouseList(lwSfpWarehouse);
        ExcelUtil<LwSfpWarehouse> util = new ExcelUtil<LwSfpWarehouse>(LwSfpWarehouse.class);
        util.exportExcel(response, list, "半成品仓库管理数据");
    }

    /**
     * 获取半成品仓库管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('sfpWarehouse:sfpWarehouse:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(lwSfpWarehouseService.selectLwSfpWarehouseById(id));
    }

    /**
     * 新增半成品仓库管理
     */
    @PreAuthorize("@ss.hasPermi('sfpWarehouse:sfpWarehouse:add')")
    @Log(title = "半成品仓库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LwSfpWarehouse lwSfpWarehouse)
    {
        return toAjax(lwSfpWarehouseService.insertLwSfpWarehouse(lwSfpWarehouse));
    }

    /**
     * 半成品仓库出库管理
     */

    @PutMapping("outSfpWarehouse")
    public AjaxResult outSfpWarehouse(@RequestBody LwSfpWarehouse lwSfpWarehouse)
    {
        int result = lwSfpWarehouseService.outSfpWarehouse(lwSfpWarehouse);
        if (result == -1) {
            return AjaxResult.error("库存不足");
        }else {
            return toAjax(result);
        }
    }

    /**
     * 修改半成品仓库管理
     */
    @PreAuthorize("@ss.hasPermi('sfpWarehouse:sfpWarehouse:edit')")
    @Log(title = "半成品仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LwSfpWarehouse lwSfpWarehouse)
    {
            return toAjax(lwSfpWarehouseService.updateLwSfpWarehouse(lwSfpWarehouse));
    }

    /**
     * 半成品出入库
     */
    @PutMapping("/updateSfpWarehouseQuantity")
    public AjaxResult updateSfpWarehouseQuantity(@RequestBody LwSfpWarehouse lwSfpWarehouse)
    {
        int result = lwSfpWarehouseService.updateLwSfpWarehouseQuantity(lwSfpWarehouse);
        if (result == -1) {
            return AjaxResult.error("参数有误");
        } else if (result == -2) {
            return AjaxResult.error("库存不足");
        } else {
            return toAjax(result);
        }
    }

    /**
     * 删除半成品仓库管理
     */
    @PreAuthorize("@ss.hasPermi('sfpWarehouse:sfpWarehouse:remove')")
    @Log(title = "半成品仓库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(lwSfpWarehouseService.deleteLwSfpWarehouseByIds(ids));
    }
}
